from rest_framework.views import APIView
from meiduo_admin.serializer.login_serializer import *
from rest_framework.response import Response


class LoginView(APIView):

    def post(self, request):
        # 使用序列化器实现用户传统登录, token签发

        # 1.构建序列化器对象
        serializer = LoginSerializer(data=request.data)
        # 2.校验
        serializer.is_valid(raise_exception=True)
        # 3.序列化返回
        # return Response(s.data)
        return Response({
            'username': serializer.validated_data['user'].username,
            'user_id': serializer.validated_data['user'].id,
            'token': serializer.validated_data['token']
        })